/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * altaYmodMedicamento.java
 *
 * Created on 01-may-2011, 12:21:53
 */



package interfaces.farmacia;
import Controlador.Controlador;
import GestorFarmacia.Medicamento;
import clinica.Interfaz;
import clinica.ClinicaView;
import clinica.Interfaces;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.ParseException;
import org.jdesktop.application.Action;
import Definiciones.Definiciones;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Ana Ferreira Blanco
 */
public class altaYmodMedicamento extends javax.swing.JPanel {

    private ClinicaView app;
    private Interfaces anterior;
    private Boolean alta, ver;
    private Medicamento medicamento;
    private Medicamento busqueda;


    /** Creates new form altaYmodMedicamento */
    public altaYmodMedicamento(ClinicaView a) {
        app = a;
        initComponents();
    }

    public void limpiaDatos() {
        nombre.setText("");
        dci_marca.setText("");
        dosis.setText("");
        umbral.setText("");
        cantidad.setText("");
        desplegable1.setSelectedIndex(0);
        desplegable2.setSelectedIndex(0);
        desplegable3.setSelectedIndex(0);
    }



    public void rellenaDatos1(Interfaces ant) throws SQLException, IOException, ClassNotFoundException, ParseException {
        limpiaDatos();
        anterior=ant;
        alta=true;
        ver = false;
        nombre.setEditable( true );
        dci_marca.setEditable( true );
        dosis.setEditable( true );
        cantidad.setEditable( true );
        umbral.setEditable( true );
        desplegable1.enable();
        desplegable2.enable();
        desplegable3.disable();
        cancelar.setVisible(true);
        
    }

    public void rellenaDatos2(Medicamento m, Interfaces ant, Medicamento b) throws SQLException, IOException, ClassNotFoundException, ParseException {
        anterior=ant;
        alta=false;
        ver = false;
        medicamento=m;
        busqueda=b;
        rellenaDatos(anterior);

    }

    public void rellenaDatos3(Medicamento m, Interfaces ant) throws SQLException, IOException, ClassNotFoundException, ParseException {
        anterior=ant;
        alta=false;
        ver=true;
        medicamento=m;
        rellenaDatos(anterior);

    }

    private void rellenaDatos(Interfaces anterior) {
        Definiciones.ATC categoria = null;
        Definiciones.ViaAdministracion via = null;
        Definiciones.Estado estado = null;

        categoria= medicamento.getCategoriaMedicamento();
            if(categoria == Definiciones.ATC.A)
                    desplegable1.setSelectedIndex(0);
            else if(categoria == Definiciones.ATC.B)
                    desplegable1.setSelectedIndex(1);
            else if(categoria == Definiciones.ATC.C)
                    desplegable1.setSelectedIndex(2);
            else if(categoria == Definiciones.ATC.D)
                    desplegable1.setSelectedIndex(3);
            else if(categoria == Definiciones.ATC.G)
                    desplegable1.setSelectedIndex(4);
            else if(categoria == Definiciones.ATC.H)
                    desplegable1.setSelectedIndex(5);
            else if(categoria == Definiciones.ATC.J)
                    desplegable1.setSelectedIndex(6);
            else if(categoria == Definiciones.ATC.L)
                    desplegable1.setSelectedIndex(7);
            else if(categoria == Definiciones.ATC.M)
                    desplegable1.setSelectedIndex(8);
            else if(categoria == Definiciones.ATC.N)
                    desplegable1.setSelectedIndex(9);
            else if(categoria == Definiciones.ATC.P)
                    desplegable1.setSelectedIndex(10);
            else if(categoria == Definiciones.ATC.R)
                    desplegable1.setSelectedIndex(11);
            else if(categoria == Definiciones.ATC.S)
                    desplegable1.setSelectedIndex(12);
            else if(categoria == Definiciones.ATC.V)
                    desplegable1.setSelectedIndex(13);


            via=medicamento.getViaAdministracion();

            if(via == Definiciones.ViaAdministracion.OFTALMICA)
                desplegable2.setSelectedIndex(0);
            else if (via == Definiciones.ViaAdministracion.OPTICA)
                desplegable2.setSelectedIndex(1);
            else if(via == Definiciones.ViaAdministracion.NASAL)
                desplegable2.setSelectedIndex(2);
            else if(via == Definiciones.ViaAdministracion.ORAL)
                desplegable2.setSelectedIndex(3);
            else if(via == Definiciones.ViaAdministracion.ORALAREOSOL)
                desplegable2.setSelectedIndex(4);
            else if(via == Definiciones.ViaAdministracion.TOPICA)
                desplegable2.setSelectedIndex(5);
            else if(via == Definiciones.ViaAdministracion.PARENTERAL)
                desplegable2.setSelectedIndex(6);
            else if(via == Definiciones.ViaAdministracion.RECTAL)
                desplegable2.setSelectedIndex(7);
            else if(via == Definiciones.ViaAdministracion.VAGINAL)
                desplegable2.setSelectedIndex(8);
            else if(via == Definiciones.ViaAdministracion.OTRA)
                desplegable2.setSelectedIndex(9);

            estado=medicamento.getEstadoMedicamento();

            if(estado == Definiciones.Estado.DISPONIBLE)
                desplegable3.setSelectedIndex(0);
            else if(estado == Definiciones.Estado.NO_DISPONIBLE)
                desplegable3.setSelectedIndex(1);

            nombre.setText(medicamento.getNombreMedicamento());
            dci_marca.setText(medicamento.getDCI());
            dosis.setText(medicamento.getDosisPorUnidad().toString());
            cantidad.setText(medicamento.getCantidadTipicaMedicamento().toString());
            umbral.setText(medicamento.getUmbralExistenciasMedicamento().toString());

            if (ver==true){
                nombre.setEditable( false );
                dci_marca.setEditable( false );
                dosis.setEditable( false );
                cantidad.setEditable( false );
                umbral.setEditable( false );
                desplegable1.disable();
                desplegable2.disable();
                desplegable3.disable();
                cancelar.setVisible(false);
            } else {
               nombre.setEditable( true );
               dci_marca.setEditable( true );
               dosis.setEditable( true );
               cantidad.setEditable( true );
               umbral.setEditable( true );
               desplegable1.enable();
               desplegable2.enable();
               desplegable3.enable();
               cancelar.setVisible(true);
            }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        nombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        desplegable1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        dci_marca = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        dosis = new javax.swing.JTextField();
        umbral = new javax.swing.JTextField();
        desplegable2 = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        cantidad = new javax.swing.JTextField();
        desplegable3 = new javax.swing.JComboBox();
        cancelar = new javax.swing.JButton();
        aceptar = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(780, 470));
        setPreferredSize(new java.awt.Dimension(780, 470));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize()));
        jPanel1.setName("jPanel1"); // NOI18N

        nombre.setFont(Interfaz.setFontSize());
        nombre.setName("nombre"); // NOI18N

        jLabel2.setFont(Interfaz.setFontSize());
        jLabel2.setText("Nombre:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel1.setFont(Interfaz.setFontSize());
        jLabel1.setText("Categoría ATC:");
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 89, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 320, Short.MAX_VALUE)
        );

        jPanel3.setName("jPanel3"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 92, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 319, Short.MAX_VALUE)
        );

        desplegable1.setFont(Interfaz.setFontSize());
        desplegable1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "A", "B", "C", "D", "G", "H", "J", "L", "M", "N", "P", "R", "S", "V" }));
        desplegable1.setName("desplegable1"); // NOI18N

        jLabel3.setFont(Interfaz.setFontSize());
        jLabel3.setText("DCI o marca:");
        jLabel3.setName("jLabel3"); // NOI18N

        dci_marca.setFont(Interfaz.setFontSize());
        dci_marca.setName("dci_marca"); // NOI18N

        jLabel4.setFont(Interfaz.setFontSize());
        jLabel4.setText("Via Administración:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(Interfaz.setFontSize());
        jLabel5.setText("Dosis por unidad");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(Interfaz.setFontSize());
        jLabel6.setText("Umbral existencias");
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setFont(Interfaz.setFontSize());
        jLabel7.setText("Cantidad típica");
        jLabel7.setName("jLabel7"); // NOI18N

        dosis.setFont(Interfaz.setFontSize());
        dosis.setName("dosis"); // NOI18N

        umbral.setFont(Interfaz.setFontSize());
        umbral.setName("umbral"); // NOI18N

        desplegable2.setFont(Interfaz.setFontSize());
        desplegable2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "OFTALMICA", "OPTICA", "NASAL", "ORAL", "ORALAREOSOL", "TOPICA", "PARENTERAL", "RECTAL", "VAGINAL", "OTRA" }));
        desplegable2.setName("desplegable2"); // NOI18N

        jPanel4.setName("jPanel4"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 728, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jPanel5.setName("jPanel5"); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 500, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jLabel8.setFont(Interfaz.setFontSize());
        jLabel8.setText("Estado");
        jLabel8.setName("jLabel8"); // NOI18N

        cantidad.setFont(Interfaz.setFontSize());
        cantidad.setName("cantidad"); // NOI18N

        desplegable3.setFont(Interfaz.setFontSize());
        desplegable3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DISPONIBLE", "NO DISPONIBLE" }));
        desplegable3.setName("desplegable3"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
                                .addGap(65, 65, 65)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(desplegable3, 0, 196, Short.MAX_VALUE)
                                    .addComponent(cantidad, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                                    .addComponent(desplegable1, 0, 196, Short.MAX_VALUE)
                                    .addComponent(nombre, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                                    .addComponent(dci_marca, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                                    .addComponent(desplegable2, 0, 196, Short.MAX_VALUE)
                                    .addComponent(dosis, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                                    .addComponent(umbral, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE))
                                .addGap(90, 90, 90)
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(desplegable1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(dci_marca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(desplegable2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel5)
                                    .addComponent(dosis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel6)
                                    .addComponent(umbral, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel7)
                                    .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(desplegable3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel8)))
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(altaYmodMedicamento.class, this);
        cancelar.setAction(actionMap.get("cancelar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(altaYmodMedicamento.class);
        cancelar.setIcon(resourceMap.getIcon("cancelar.icon")); // NOI18N
        cancelar.setLabel(resourceMap.getString("cancelar.label")); // NOI18N
        cancelar.setName(""); // NOI18N

        aceptar.setAction(actionMap.get("aceptar")); // NOI18N
        aceptar.setIcon(resourceMap.getIcon("aceptar.icon")); // NOI18N
        aceptar.setLabel(resourceMap.getString("cancelar.label")); // NOI18N
        aceptar.setName(""); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cancelar)
                        .addGap(18, 18, 18)
                        .addComponent(aceptar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(aceptar)
                    .addComponent(cancelar))
                .addGap(22, 22, 22))
        );
    }// </editor-fold>//GEN-END:initComponents
    @Action
    public void aceptar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException, ParseException {
        Definiciones.ATC categoria = null;
        Definiciones.ViaAdministracion via = null;
        Definiciones.Estado estado = null;
        Boolean error = false;

        if (ver) {
            ver=false;
            app.volver(Interfaces.BUSCAR_MEDICAMENTO_2);
            return;
        }
        //Si alta o modificar => leemos los campos
            if(desplegable1.getSelectedIndex()==0)
                categoria = Definiciones.ATC.A;
            else if(desplegable1.getSelectedIndex()==1)
                categoria = Definiciones.ATC.B;
            else if(desplegable1.getSelectedIndex()==2)
                categoria = Definiciones.ATC.C;
            else if(desplegable1.getSelectedIndex()==3)
                categoria = Definiciones.ATC.D;
            else if(desplegable1.getSelectedIndex()==4)
                categoria = Definiciones.ATC.G;
            else if(desplegable1.getSelectedIndex()==5)
                categoria = Definiciones.ATC.H;
            else if(desplegable1.getSelectedIndex()==6)
                categoria = Definiciones.ATC.J;
            else if(desplegable1.getSelectedIndex()==7)
                categoria = Definiciones.ATC.L;
            else if(desplegable1.getSelectedIndex()==8)
                categoria = Definiciones.ATC.M;
            else if(desplegable1.getSelectedIndex()==9)
                categoria = Definiciones.ATC.N;
            else if(desplegable1.getSelectedIndex()==10)
                categoria = Definiciones.ATC.P;
            else if(desplegable1.getSelectedIndex()==11)
                categoria = Definiciones.ATC.R;
            else if(desplegable1.getSelectedIndex()==12)
                categoria = Definiciones.ATC.S;
            else if(desplegable1.getSelectedIndex()==13)
                categoria = Definiciones.ATC.V;

            if(desplegable2.getSelectedIndex()==0)
                via = Definiciones.ViaAdministracion.OFTALMICA;
            else if(desplegable2.getSelectedIndex()==1)
                via = Definiciones.ViaAdministracion.OPTICA;
            else if(desplegable2.getSelectedIndex()==2)
                via = Definiciones.ViaAdministracion.NASAL;
            else if(desplegable2.getSelectedIndex()==3)
                via = Definiciones.ViaAdministracion.ORAL;
            else if(desplegable2.getSelectedIndex()==4)
                via = Definiciones.ViaAdministracion.ORALAREOSOL;
            else if(desplegable2.getSelectedIndex()==5)
                via = Definiciones.ViaAdministracion.TOPICA;
            else if(desplegable2.getSelectedIndex()==6)
                via = Definiciones.ViaAdministracion.PARENTERAL;
            else if(desplegable2.getSelectedIndex()==7)
                via = Definiciones.ViaAdministracion.RECTAL;
            else if(desplegable2.getSelectedIndex()==8)
                via = Definiciones.ViaAdministracion.VAGINAL;
            else if(desplegable2.getSelectedIndex()==9)
                via = Definiciones.ViaAdministracion.OTRA;

            if(desplegable3.getSelectedIndex()==0)
                estado = Definiciones.Estado.DISPONIBLE;
            else if(desplegable3.getSelectedIndex()==1)
                estado = Definiciones.Estado.NO_DISPONIBLE;

        // Comprobamos posibles errores

        Pattern p = Pattern.compile("[^0-9]+");
        Pattern l = Pattern.compile("[^A-Za-z0-9 áéíóúÁÉÍÓÚñÑ]+");
        Matcher m = l.matcher(nombre.getText());




        if (nombre.getText().isEmpty()) {
            error = true;
            app.addError("Debe introducir un nombre.");
        }
        if (m.find()) {
            error = true;
            app.addError("El nombre contiene caracteres no permitidos. Use solo letras y números.");
        }

        m = l.matcher(dci_marca.getText());

        if (dci_marca.getText().isEmpty()) {
            error = true;
            app.addError("Debe introducir un DCI o una marca.");
        }
        if (m.find()) {
            error = true;
            app.addError("La marca contiene caracteres no permitidos. Use solo letras y números.");
        }


        m = p.matcher(dosis.getText().toString());

        if (dosis.getText().isEmpty()) {
            error = true;
            app.addError("Debe introducir una dosis.");
        }
        if (m.find()) {
            error = true;
            app.addError("La dosis debe ser una cifra");
        }
        if (Integer.parseInt(dosis.getText())<=0){
            error = true;
            app.addError("La dosis debe ser superior a 0");
        }
        if (dosis.getText().length()>2){
            error = true;
            app.addError("La dosis no es válida.");
        }

        m = p.matcher(umbral.getText().toString());

        if (umbral.getText().isEmpty()) {
            error = true;
            app.addError("Debe introducir un umbral.");
        }

        if (m.find()) {
            error = true;
            app.addError("El umbral debe ser una cifra");
        }
        if (Integer.parseInt(umbral.getText())<=0){
            error = true;
            app.addError("El umbral debe ser superior a 0");
        }
        if (umbral.getText().length()>3){
            error = true;
            app.addError("El umbral no es válido.");
        }
        if (Integer.parseInt(umbral.getText()) < Integer.parseInt(dosis.getText())){
            error = true;
            app.addError("El umbral no puede ser inferior a la dosis.");
        }

        m = p.matcher(cantidad.getText().toString());

        if (cantidad.getText().isEmpty()) {
            error = true;
            app.addError("Debe introducir una cantidad.");
        }
        if (m.find()) {
            error = true;
            app.addError("La cantidad de ser una cifra");
        }
        if (Integer.parseInt(cantidad.getText())<=0){
            error = true;
            app.addError("La cantidad debe ser superior a 0");
        }
        if (cantidad.getText().length()>3){
            error = true;
            app.addError("La cantidad no es válida.");
        }
        if (Integer.parseInt(cantidad.getText()) < Integer.parseInt(umbral.getText())){
            error = true;
            app.addError("La cantidad no puede ser inferior al umbral");
        }
        if (Integer.parseInt(cantidad.getText()) < Integer.parseInt(dosis.getText())){
            error = true;
            app.addError("La cantidad no puede ser inferior a la dosis");
        }



        // Salida oportunas: 1.Error|2.Alta|3.Modificar
        if (error) { //Error
            app.muestraListaErrores();
            app.limpiaErrores();
        } else if(alta) {// Alta
            Medicamento[] existentes = Controlador.buscarMedicamento(null, categoria, nombre.getText(), dci_marca.getText(),via,Integer.parseInt(dosis.getText()), Integer.parseInt(umbral.getText()),Integer.parseInt(cantidad.getText()), estado);
            if(existentes != null)//Compruebo que el medicamento no existe ya en la BD
                app.abrirMensajeError("El medicamento ya existe.",anterior);
            else{//Si no existe lo añado
                String res =  Controlador.altaMedicamento(categoria, nombre.getText(),dci_marca.getText(),via,Integer.parseInt(dosis.getText()), Integer.parseInt(umbral.getText()),Integer.parseInt(cantidad.getText()));
                if(res==null) app.abrirMensajeExito("Medicamento dado de alta con éxito.", Interfaces.PRINCIPAL_FARMACEUTICO);
                else app.abrirMensajeError(res, Interfaces.PRINCIPAL_FARMACEUTICO);
            }
        } else { // Modificar
            Medicamento[] existentes = Controlador.buscarMedicamento(null, categoria, nombre.getText(), dci_marca.getText(),via,Integer.parseInt(dosis.getText()), Integer.parseInt(umbral.getText()),Integer.parseInt(cantidad.getText()), estado);
            if(existentes != null)//Compruebo que el medicamento no existe ya en la BD
                app.abrirMensajeError("No ha introducido ningún cambio.",anterior);
            else{//Si no existe lo añado
                String res = Controlador.modificarMedicamento (medicamento.getIdMedicamento(), categoria, nombre.getText(),dci_marca.getText(), via, Integer.parseInt(dosis.getText()),Integer.parseInt(umbral.getText()), Integer.parseInt(cantidad.getText()), estado);
                if(res==null) app.abrirMensajeExito("Medicamento modificado con éxito.", Interfaces.BUSCAR_MEDICAMENTO_2, busqueda);
                else app.abrirMensajeError(res, Interfaces.BUSCAR_MEDICAMENTO_2);
            }
        }
    }

    @Action
    public void cancelar() {
        app.volver(anterior);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptar;
    private javax.swing.JButton cancelar;
    private javax.swing.JTextField cantidad;
    private javax.swing.JTextField dci_marca;
    private javax.swing.JComboBox desplegable1;
    private javax.swing.JComboBox desplegable2;
    private javax.swing.JComboBox desplegable3;
    private javax.swing.JTextField dosis;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTextField nombre;
    private javax.swing.JTextField umbral;
    // End of variables declaration//GEN-END:variables

}
